package com.bonc.modules.item.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.bonc.common.param.BaseParam;
import com.bonc.common.utils.Result;
import com.bonc.modules.common.entity.Region;
import com.bonc.modules.common.entity.StdCode;
import com.bonc.modules.common.service.CommonService;
import com.bonc.modules.common.service.StdCodeService;
import com.bonc.modules.item.entity.ItemValueEntity;
import com.bonc.modules.item.param.ItemValueParam;
import com.bonc.modules.item.result.ItemChartDataResult;
import com.bonc.modules.item.service.ItemValueService;
import com.bonc.modules.rpt.entity.ServUpgTsSumMon;
import com.bonc.modules.rpt.service.ServUpgTsSumMonServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @author jinlong
 * @date 2021/8/6
 */
@RestController
@RequestMapping(value = "/item-value")
public class ItemValueController {

    @Autowired
    private ItemValueService itemValueService;

    @Autowired
    private ServUpgTsSumMonServ servUpgTsSumMonServ;

    @Autowired
    private StdCodeService stdCodeService;

    @Autowired
    private CommonService commonService;


    /**
     * @param itemValueParam
     * @return
     */
    @PostMapping("/getChartText")
    public Result getChartText(@RequestBody @Validated({BaseParam.list.class}) ItemValueParam itemValueParam) {
        ItemValueEntity textData = itemValueService.getTextData(itemValueParam);
        return Result.ok().put("data", textData);
    }
    /**
     * @param itemValueParam
     * @return
     */
    @PostMapping("/getUpgTextData")
    public Result getUpgTextData(@RequestBody @Validated({BaseParam.list.class}) ItemValueParam itemValueParam) {
        ServUpgTsSumMon servUpgTsSumMon = servUpgTsSumMonServ.getUpgTextData(itemValueParam);
        return Result.ok().put("data", servUpgTsSumMon);
    }

    /**
     * 获取图数据
     *
     * @return
     */
    @PostMapping("/getChartDataDistable/{chartType}")
    public Result getChartDataDistable(@RequestBody @Validated(BaseParam.list.class) ItemValueParam itemValueParam, @PathVariable String chartType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, StdCode> chartTypeMap = stdCodeService.getConfMap("CHART_TYPE");
        StdCode stdCode = MapUtil.get(chartTypeMap, chartType, StdCode.class);
        Method getChartData = itemValueService.getClass().getMethod(stdCode.getStdConf(), ItemValueParam.class);
        ItemChartDataResult chartDataResult = (ItemChartDataResult) getChartData.invoke(itemValueService, itemValueParam);
        return Result.ok().put("data", chartDataResult);
    }

    /**
     * 获取图数据
     *
     * @return
     */
    @PostMapping("/getChartData/{chartType}")
    public Result getChartData(@RequestBody @Validated(BaseParam.list.class) ItemValueParam itemValueParam, @PathVariable String chartType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, StdCode> chartTypeMap = stdCodeService.getConfMap("CHART_TYPE");
        StdCode stdCode = MapUtil.get(chartTypeMap, chartType, StdCode.class);
        //Method getChartData = itemValueService.getClass().getMethod(stdCode.getStdConf(), ItemValueParam.class);
        //ItemChartDataResult chartDataResult = (ItemChartDataResult) getChartData.invoke(itemValueService, itemValueParam);

        try {
            Integer index = stdCode.getStdConf().lastIndexOf(".");
            String className = stdCode.getStdConf().substring(0, index);
            String methodName = stdCode.getStdConf().substring(index + 1);

            Class clazz = Class.forName(className);
            Method getChartData = clazz.getMethod(methodName, ItemValueParam.class);
            ItemChartDataResult chartDataResult = (ItemChartDataResult) getChartData.invoke(SpringUtil.getBean(clazz), itemValueParam);

            if("getRegionChart".equals(methodName)){
                String regionStaticCode = "00";
                List<Region> regionList = null;
                int regionType = 0;
                Region regionParam = new Region();
                if (ObjectUtil.isNotEmpty(itemValueParam.getGridCode()) && !regionStaticCode.equals(itemValueParam.getGridCode())) {
                    // 同级网格 getGrid
                    regionType = 2;
                    regionParam.setCityCode(itemValueParam.getCityCode());
                    regionParam.setCountyCode(itemValueParam.getCountyCode());
                    regionList = commonService.getGrid(regionParam);
                } else if (ObjectUtil.isNotEmpty(itemValueParam.getCountyCode()) && !regionStaticCode.equals(itemValueParam.getCountyCode())) {
                    // 同级区县 getCounty
                    regionType = 1;
                    regionParam.setCityCode(itemValueParam.getCityCode());
                    regionList = commonService.getCounty(regionParam);
                } else {
                    // 同级地市  getCity
                    regionType = 0;
                    regionList =commonService.getCity(regionParam);
                }

                if(regionList != null){
                    List<String> xAxisList = chartDataResult.getXAxis();
                    for(int i = 0; i < xAxisList.size(); i++){
                        for(Region region : regionList){
                            if(regionType == 1 && xAxisList.get(i).trim().equals(region.getCountyCode().trim())){
                                xAxisList.set(i, region.getCountyName());
                                break;
                            } else if(regionType == 2 && xAxisList.get(i).trim().equals(region.getGridCode().trim())){
                                xAxisList.set(i, region.getGridName());
                                break;
                            } else if(regionType == 0 && xAxisList.get(i).trim().equals(region.getCityCode().trim())){
                                xAxisList.set(i, region.getCityName());
                                break;
                            }
                        }
                    }
                }
            }
            return Result.ok().put("data", chartDataResult);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return Result.error();
    }
}
